package smlab.view.ui.page
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.text.TextField;
	
	[Event(name="change",type="flash.events.Event")]
	
	/**
	 * 翻页组件
	 * @author simon
	 * 
	 * 监听 Event.CHANGE 来获取 CurrentPageData 当前页数据
	 * 
	 */
	public class page_SM extends EventDispatcher
	{
		private var _dataProvider:Array=[];//所有数据
		private var _skin:DisplayObjectContainer;
		private var _nextBtn:InteractiveObject;//向右或向上翻页按钮
		private var _prevBtn:InteractiveObject;//向左或向下翻页按钮
		private var _txt:TextField;//显示页码的文本
		private var _amountPerPaget:uint=0;//每页多少项数据
		private var _currentPage:uint=0;//当前页码
		private var _totalPage:uint=0;//总页数
		private var _pageDataArray:Array=[];//所有页数据
		private var _currentPageData:Array;//当前页数据
		private var _wrap:Boolean=true;//到达最后一页是否翻转
		private var _autoHideBtn:Boolean=false;//到达最后一页是否隐藏按钮
		private var _autoGrayBtn:Boolean=false;//到达最后一页是否灰掉按钮
		
		/**
		 * 构造函数
		 * @param dataProvider		数据
		 * @param amountPerPage		每页多少项
		 * @param prevBtn			左翻页(后退)按钮
		 * @param nextBtn			右翻页(向前)按钮
		 * @param pageTxt			显示页码文本
		 * 
		 */
		public function page_SM(dataProvider:Array=null,amountPerPage:uint=1,prevBtn:InteractiveObject=null,nextBtn:InteractiveObject=null,pageTxt:TextField=null)
		{
			super();
			PrevBtn=prevBtn;
			NextBtn=nextBtn;
			PageTxt=pageTxt;
			AmountPerPaget=amountPerPage;
			DataProvider=dataProvider;
		}
		
		/**
		 * 设置翻页按钮是否到头后隐藏
		 * @param data
		 * 
		 */ 
		public function set autoHideBtn(b:Boolean):void{
			_autoHideBtn=b;
			checkBtn();
		}
		
		public function get autoHideBtn():Boolean{
			return _autoHideBtn;
		}
		
		/**
		 * 设置翻页按钮是否到头后变灰
		 * @param data
		 * 
		 */ 
		public function set autoGrayBtn(b:Boolean):void{
			_autoGrayBtn=b;
			checkBtn();
		}
		
		public function get autoGrayBtn():Boolean{
			return _autoGrayBtn;
		}
		
		/**
		 * 设置左翻页(后退)按钮
		 * @param data
		 * 
		 */ 
		public function set PrevBtn(b:InteractiveObject):void{
			_prevBtn=b;
		}
		
		public function get PrevBtn():InteractiveObject{
			return _prevBtn;
		}
		
		/**
		 * 设置右翻页(向前)按钮
		 * @param data
		 * 
		 */
		public function set NextBtn(b:InteractiveObject):void{
			_nextBtn=b;
		}
		
		public function get NextBtn():InteractiveObject{
			return _nextBtn;
		}
		
		/**
		 * 设置显示页码文本
		 * @param data
		 * 
		 */
		public function set PageTxt(t:TextField):void{
			_txt=t;
			txtChange();
		}
		
		public function get PageTxt():TextField{
			return _txt;
		}
		
		/**
		 * 设置数据驱动
		 * @param data
		 * 
		 */
		public function set DataProvider(data:Array):void{
			_dataProvider=data;
			update();
		}
		
		public function get DataProvider():Array{
			return _dataProvider;
		}
		
		public function set CurrentPageData(data:Array):void{
			_currentPageData=data;
		}
		
		public function get CurrentPageData():Array{
			return _currentPageData;
		}
		
		/**
		 * 获取当前页码
		 * @return 
		 * 
		 */
		public function get CurrentPage():uint{
			return _currentPage;
		}
		
		/**
		 * 获取总页数
		 * @return 
		 * 
		 */
		public function get TotalPage():uint{
			return _totalPage;
		}
		
		/**
		 * 设置每页多少项
		 * @param n
		 * 
		 */
		public function set AmountPerPaget(n:uint):void{
			_amountPerPaget=n;
		}
		
		public function get AmountPerPaget():uint{
			return _amountPerPaget;
		}
		
		/**
		 * 到达最后1页是否可以继续翻页（回到第1页）
		 * @param b
		 * 
		 */
		public function set Wrap(b:Boolean):void{
			_wrap=b;
			checkBtn();
		}
		
		public function get Wrap():Boolean{
			return _wrap;
		}
		
		public function get Skin():DisplayObjectContainer{
			return _skin;
		}
		
		/**
		 * 更新数据
		 * 
		 */
		public function update():void
		{
			if(!_dataProvider){
				return; 
			}
			_totalPage=Math.ceil(_dataProvider.length/_amountPerPaget);
			_pageDataArray=[];
			
			for(var i:int=0;i<_totalPage;i++){
				var a:Array=_dataProvider.slice(i*_amountPerPaget,_amountPerPaget*(i+1));
				_pageDataArray.push(a);
			}
			
			if(_totalPage>0)
			{
				_currentPage = 1;
			}else{
				_currentPage = 0;
			}
			
			if(_pageDataArray.length>0){
				CurrentPageData=_pageDataArray[_currentPage-1];
			}
			
			checkBtn();
			txtChange();
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		
		/**
		 * 跳到某页
		 * 如果跳到的是当前页，不做任何事
		 * @param index	从1开始
		 * 
		 */
		public function JumpTopage(index:uint):void
		{
			if(index<=0 || index>this.DataProvider.length){
				return;
			}
			
			_currentPage = index;
			jumPage();
		}
		
		private function onPageBtnClick(e:MouseEvent):void
		{
			var doChange:Boolean=true;
			
			if(e.currentTarget==NextBtn){
				_currentPage++;
			}else{
				_currentPage--;
			}
			
			jumPage(doChange);
		}
		
		private function jumPage(doChange:Boolean=true):void
		{
			if(_currentPage>_totalPage){
				if(Wrap){
					_currentPage=1;
				}else{
					_currentPage=_totalPage;
					doChange=false;
				}
			}
			
			if(_currentPage<1){
				if(Wrap){
					_currentPage=_totalPage;
				}else{
					_currentPage=1;
					doChange=false;
				}
			}
			
			checkBtn();
			CurrentPageData=_pageDataArray[_currentPage-1];
			
			if(doChange){
				txtChange();
				this.dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		private function checkBtn():void
		{
			if(TotalPage == 1 || TotalPage == 0)
			{
				setBtnStatus(NextBtn,false);
				setBtnStatus(PrevBtn,false);
			}
			else
			{
				if(CurrentPage == 1)
				{
					setBtnStatus(NextBtn,true);
					setBtnStatus(PrevBtn,false);
				}
				else if(CurrentPage == TotalPage)
				{
					setBtnStatus(NextBtn,false);
					setBtnStatus(PrevBtn,true);
				}
				else
				{
					setBtnStatus(NextBtn,true);
					setBtnStatus(PrevBtn,true);
				}
			}
		}
		
		private function setBtnStatus(btn:InteractiveObject,enabled_:Boolean):void
		{
			if(btn)
			{
				if(enabled_){
					btn.addEventListener(MouseEvent.CLICK,onPageBtnClick);
					btn.mouseEnabled=true;
					btn.visible = true;
				}else{
					btn.removeEventListener(MouseEvent.CLICK,onPageBtnClick);
					btn.mouseEnabled=false;
					btn.visible = !autoHideBtn;
				}
				
				if(btn.visible)
				{
					if(enabled_){
						btn.filters=[];
						btn.visible=true;
					}else{
						if(autoGrayBtn)
						{
							setGrey(btn);
						}
					}
				}
			}
		}
		
		private function txtChange():void{
			if(_txt){
				_txt.text=_currentPage+"/"+_totalPage;
			}
		}
		
		private function setGrey(d:DisplayObject):void{
			if(d){
				var mat:Array = [0.3086,0.6094,0.082,0,0,0.3086,0.6094,0.082,0,0,0.3086,0.6094,0.082,0,0,0,0,0,1,0];
				var colorMat:ColorMatrixFilter = new ColorMatrixFilter(mat);
				d.filters = [colorMat];
			}
		}
		
		public function remove():void
		{
			_currentPageData=[];
			_dataProvider=[];
			PrevBtn=null;
			NextBtn=null;
			PageTxt=null;
		}
	}
}